package com.tanhao.takeaway.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhao.takeaway.commom.CustomException;
import com.tanhao.takeaway.entity.setmeal;
import com.tanhao.takeaway.entity.setmealDish;
import com.tanhao.takeaway.entity.setmealDto;
import com.tanhao.takeaway.mapper.setmealMapper;
import com.tanhao.takeaway.service.setmealDishService;
import com.tanhao.takeaway.service.setmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class setmealServiceImpl extends ServiceImpl<setmealMapper, setmeal> implements setmealService {

  @Autowired
  private  setmealDishService setmealdishservice;
    @Override
    @Transactional
    public void saveWithDish(setmealDto setmealdto) {
        this.save(setmealdto);

        List<setmealDish> setmealDishes = setmealdto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealdto.getId());
            return item;
        }).collect(Collectors.toList());

      setmealdishservice.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        LambdaQueryWrapper<setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(setmeal::getId,ids);
        lambdaQueryWrapper.eq(setmeal::getStatus,1);
        int count=this.count(lambdaQueryWrapper);
         if(count>0) throw new CustomException("当前有套餐正处于起售状态，无法删除");

         this.removeByIds(ids);

         LambdaQueryWrapper<setmealDish> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
         lambdaQueryWrapper1.in(setmealDish::getSetmealId,ids);
         setmealdishservice.remove(lambdaQueryWrapper1);

    }

//    @Override
//    public List<setmealDto> listWithDish(setmeal s) {
//        LambdaQueryWrapper<setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(s.getCategoryId()!=null,setmeal::getCategoryId,s.getCategoryId());
//        lambdaQueryWrapper.eq(setmeal::getStatus,s.getStatus());
//        List<setmeal> list = this.list(lambdaQueryWrapper);
//
//
//        List<setmealDto> setmealDishes = list.stream().map((item) -> {
//            setmealDto setmealdto = new setmealDto();
//            BeanUtils.copyProperties(list, setmealdto, "setmealDishes");
//            LambdaQueryWrapper<setmealDish> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper1.eq(setmealDish::getSetmealId, item.getId());
//            setmealDish one = (setmealDish) setmealdishservice.list(lambdaQueryWrapper1);
//            setmealdto.setSetmealDishes(one);
//            return setmealdto;
//        }).collect(Collectors.toList());
//
//        return setmealdto;
//    }
}
